#include "util.h"

uint64_t hash(unsigned char *str) {
    uint64_t hash = 5381;
    int c;

    while (c = *str++)
        /* hash * 33 + c */
        hash = ((hash << 5) + hash) + c; 

    return hash;
}

track* create_track(uint64_t duration, char *file, char *album_tag, char *albumartist_tag, char *artist_tag, char *comment_tag, char *date_tag, char *genre_tag, char *title_tag, uint64_t discnumber_tag, uint64_t tracknumber_tag) {
    track *track = malloc(sizeof(*track));

    track->duration = duration;
    track->file = file;
    track->album_tag = album_tag;
    track->albumartist_tag = albumartist_tag;
    track->artist_tag = artist_tag;
    track->comment_tag = comment_tag;
    track->date_tag = date_tag;
    track->genre_tag = genre_tag;
    track->title_tag = title_tag;
    track->discnumber_tag = discnumber_tag;
    track->tracknumber_tag = tracknumber_tag;

    return track;
}

void destroy_track(track *track) {
    free(track->file);
    free(track->album_tag);
    free(track->albumartist_tag);
    free(track->artist_tag);
    free(track->comment_tag);
    free(track->date_tag);
    free(track->genre_tag);
    free(track->title_tag);
    free(track);
}

void print_track(track *track) {
    printf(
        "track { duration %u, file %s, album_tag %s, albumartist_tag %s, artist_tag %s, comment_tag %s, date_tag %s, genre_tag %s, title_tag %s, discnumber_tag %u, tracknumber_tag %u }\n",
        track->duration,
        track->file,
        track->album_tag,
        track->albumartist_tag,
        track->artist_tag,
        track->comment_tag,
        track->date_tag,
        track->genre_tag,
        track->title_tag,
        track->discnumber_tag,
        track->tracknumber_tag
    );
}

player* create_player(char *aaa_mode, char *status, uint64_t vol_left, uint64_t vol_right, uint64_t position, bool repeat, bool repeat_current, bool shuffle) {
    player *player = malloc(sizeof(*player));

    player->aaa_mode = aaa_mode;
    player->status = status;
    player->vol_left = vol_left;
    player->vol_right = vol_right;
    player->position = position;
    player->repeat = repeat;
    player->repeat_current = repeat_current;
    player->shuffle = shuffle;

    return player;    
}

void destroy_player(player *player) {
    free(player->aaa_mode);
    free(player->status);
    free(player);
}

void print_player(player *player) {
    printf(
        "player { aaa_mode %s, status %s, vol_left %u, vol_right %u, position %u, repeat %i, repeat_current %i, shuffle %i }\n",
        player->aaa_mode,
        player->status,
        player->vol_left,
        player->vol_right,
        player->position,
        player->repeat,
        player->repeat_current,
        player->shuffle
    );
}

